//
//  BKODAddressCell.m
//  bestkeep
//
//  Created by yons on 16/12/19.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKODAddressCell.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"
/**
    收货地址
 */

@implementation BKODAddressCell
{
    UIImageView *_laceImg;
    UIView      *_whiteView;
    UILabel     *_name;
    UILabel     *_phone;
    UILabel     *_address;
    UIImageView *_selected;
    UIButton    *_edit;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _laceImg = [[UIImageView alloc] init];
    [self.contentView addSubview:_laceImg];
    
    _whiteView = [[UIView alloc] init];
    _whiteView.backgroundColor = kOrderWhite;
    [self.contentView addSubview:_whiteView];
    
    _name = [[UILabel alloc] init];
    _name.textColor = kOrderBlack1;
    _name.numberOfLines = 1;
    _name.font      = UIFontBoldCustom(15);
    [self.contentView addSubview:_name];
    
    _selected = [[UIImageView alloc] init];
    [self.contentView addSubview:_selected];
    /*
     * 电话
     */
    _phone = [[UILabel alloc] init];
    _phone.textColor = kOrderBlack1;
    _phone.numberOfLines = 1;
    _phone.font      = UIFontBoldCustom(15);
    [self.contentView addSubview:_phone];
    
    _address = [[UILabel alloc] init];
    _address.textColor = kOrderBlack1;
    _address.font      = UIFontNormal;
    _address.numberOfLines = 0;
    [self.contentView addSubview:_address];
    
    _edit = [UIButton buttonWithType:UIButtonTypeCustom];
    [_edit setTitle:kOrderLocalizeString(@"BKOrderEdit") forState:UIControlStateNormal];
    [_edit setTitleColor:kOrderBlack1 forState:UIControlStateNormal];
    _edit.titleLabel.font = UIFontNormal;
    [_edit setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    [_edit setImage:[UIImage imageNamed:@"BKOrder_address_edit"] forState:UIControlStateNormal];
    [_edit setImageEdgeInsets:UIEdgeInsetsMake(0, 3, 0, 5)];
    [_edit addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_edit];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_laceImg mas_makeConstraints:^(MASConstraintMaker *make){
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [_whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.right.left.mas_equalTo(self.contentView);
    }];
    
    [_selected mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).offset(ApplicationBackGauge);
        make.width.height.mas_equalTo(16);
    }];
    
   
    
//    [_phone mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(_selected.mas_right).offset(15);
//        make.top.mas_equalTo(_name.mas_bottom).offset(10);
//    }];
    
    
    
    [_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(ScreenWidth/2.0);
        make.top.mas_equalTo(_name.mas_top).offset(0);
    }];
    [_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_selected.mas_right).offset(15);
        make.right.mas_equalTo(_phone.mas_left).offset(-10);
        make.top.mas_equalTo(_whiteView.mas_top).offset(10);
    }];
    
    
//    [_address mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(_name);
//        make.bottom.mas_equalTo(_whiteView.mas_bottom).offset(-10);
//        make.width.mas_equalTo(kOrderScreenWidth - 56);
//    }];
    
    [_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_name);
//        make.bottom.mas_equalTo(_whiteView.mas_bottom).offset(-10);
        make.top.mas_equalTo(_name.mas_bottom).offset(10);
        make.width.mas_equalTo(kOrderScreenWidth - 100);
    }];
    
    
    
    CGSize btnSize = [_edit.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : UIFontNormal}];
    [_edit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(btnSize.width + 35);
        make.height.mas_equalTo(self.contentView);
    }];
}

//编辑地址
- (void)editAction:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(ODAddressCellEditAddressInfo:)]) {
        [self.delegate ODAddressCellEditAddressInfo:self];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected) {
        _selected.image = [UIImage imageNamed:@"BKOrder_selected"];
    }
    else{
        _selected.image = [UIImage imageNamed:@""];
    }
}

- (void)fillData:(id)data
{
    if (!data) {
        return;
    }
    
    BKODDetailsAddressObject *obj = data;
    
    if ([obj.is_default boolValue] == YES)
    {
        NSString *imageName =@"BKOrder_address_BG";
        _laceImg.image = [UIImage imageNamed:imageName];
        [self updateBGConstraints];
    }
    
    _name.text = obj.deliver_name;
    _phone.text = obj.telephone;

    _address.text = [NSString stringWithFormat:@"%@%@%@%@", obj.province_name, obj.city_name, obj.county_name, obj.address];

    
}

- (void)updateBGConstraints
{
    [_whiteView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(5);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
    }];
}

+ (CGFloat)computeHeight:(id)data
{
    if (!data) {
        return 0;
    }
    BKODDetailsAddressObject *obj = data;
    NSString *address = [NSString stringWithFormat:@"%@%@%@%@", obj.province_name, obj.city_name, obj.county_name, obj.address];
    CGSize textFont15Size = [@"算高度" sizeWithAttributes:@{NSFontAttributeName : UIFontBoldCustom(15)}];
    
    CGRect adsRect = [address boundingRectWithSize:CGSizeMake(kOrderScreenWidth - 56, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : UIFontNormal} context:nil];
    
    CGFloat height = adsRect.size.height + textFont15Size.height * 2 + 10 * 4 + 10;
    height = [obj.is_default boolValue] == YES ? height : height - 10;
    
    return height;
}


@end



@implementation BKODAddNewAddressCell
{
    UIButton *_addAddress;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    
    _addAddress = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addAddress setTitle:kOrderLocalizeString(@"BKOrderAddNewAddress") forState:UIControlStateNormal];
    _addAddress.titleLabel.font = UIFontCustom(18);
    _addAddress.layer.cornerRadius = 5;
    _addAddress.layer.masksToBounds = YES;
    UIImage *BGImage = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:kOrderBKGreen];
    [_addAddress setBackgroundImage:BGImage forState:UIControlStateNormal];
    [_addAddress addTarget:self action:@selector(addNewAddress:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_addAddress];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_addAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(5, 20, 5, 20));
        
    }];
}

- (void)addNewAddress:(UIButton *)sender
{
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODAddNewAddressCellDelegate)]) {
        [self.delegate ODCellAddAddress:self];
    }
}

- (void)fillData:(id)data
{
    
}

+ (CGFloat)computeHeight:(id)data
{
    return 58;
}

@end
